<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Share extends Model
{
    protected $fillable = [
        'parent_id',
        'user_id',
        'code',
    ];

    /**
     * 邀请的下级
     */
    public function children(): HasMany
    {
        return $this->hasMany(Share::class, 'parent_id');
    }

    /**
     * 上级推荐人
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(Share::class);
    }

    /**
     * 对应拥有邀请码的用户
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}
